/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package banksystem;
import java.util.Iterator;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ROBM
 */
public class ViewFrame extends javax.swing.JFrame {

	Controller mController;
	Account mAccount;
	int tableRows;
	/**
	 * Creates new form ViewFrame
	 */
	public ViewFrame(Controller controller, Account account) {
		tableRows = account.getTransactionList().size();
		initComponents();
		this.mAccount = account;
		this.mController = controller;
		
		this.accountLabel.setText(this.mAccount.getName());
		this.balanceLabel.setText(this.balanceLabel.getText() + this.mAccount.getBalance());
		
		Transaction t;
		int size = (account.getTransactionList().size() - 1) ;
		for(int i = 0; i <= size; i++)
		{
			t = account.getTransactionList().get(size - i);
			this.jTable1.getModel().setValueAt(t.getDate(), i,0);
			this.jTable1.getModel().setValueAt(t.getType(), i,1);
			this.jTable1.getModel().setValueAt(new Double(t.getAmount()), i,2);
			
			if(t.getClass() == TransferTransaction.class)
			{
				TransferTransaction transfer = (TransferTransaction) t;
				String str;
				
				if(account == transfer.getAccountFrom())
				{
					str = "To: " + transfer.getAccountTo().getName();// if from, display to
				}else{
					str = "From: " + transfer.getAccountFrom().getName();//if to display from
				}

				this.jTable1.getModel().setValueAt(str, i,3);
			}
		}
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        accountLabel = new javax.swing.JLabel();
        balanceLabel = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        accountLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        accountLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        accountLabel.setText("Account:");
        accountLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        balanceLabel.setText("Balance: ");

        okButton.setText("Ok");
        okButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                okButtonMouseClicked(evt);
            }
        });

        jTable1.setModel(new DefaultTableModel(new Object[]{"Date", "Type", "Amount", ""}, tableRows));
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(balanceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(okButton))
                        .addGap(0, 165, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(accountLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(accountLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(balanceLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addComponent(okButton)
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void okButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_okButtonMouseClicked
		this.mController.SaveAndRestart(this);
	}//GEN-LAST:event_okButtonMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel accountLabel;
    private javax.swing.JLabel balanceLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables
}
